package util;

import java.io.File;
import java.lang.reflect.Field;

import basic.FieldMeta;
import entities.TBrand;
import utils.FileUtil;
import utils.StringUtil;

public class TemplateUtil {

	private String tableName;
	private String beanName;
	private String nickName;
	private String headName;
	private String filePath;

	public TemplateUtil(String tableName, String beanName, String nickName, String headName, String filePath) {
		this.tableName = tableName;
		this.beanName = beanName;
		this.nickName = nickName;
		this.headName = headName;
		this.filePath = filePath;
	}

	public <E> void generate(Class<E> target) throws Exception {
		String list = generateList(target);
		String edit = generateEdit(target);
		String save = generateSave(target);
		String info = generateInfo(target);
		String show = generateShow(target);
		String action = generateAction(target);
		FileUtil.write(filePath, "list.jsp", list);
		FileUtil.write(filePath, "edit.jsp", edit);
		FileUtil.write(filePath, "save.jsp", save);
		FileUtil.write(filePath, "info.jsp", info);
		FileUtil.write(filePath, "show.jsp", show);
		FileUtil.write(filePath, beanName + "Action.java", action);
	}

	public <E> String generateList(Class<E> target) throws Exception {
		Field[] fields = target.getDeclaredFields();
		File file = new File(this.getClass().getResource("/template/list.template").getFile());
		String content = FileUtil.read(file);
		content = content.replaceAll("\\[condition\\]", getCondition(fields));
		content = content.replaceAll("\\[sort\\]", getSort(fields));
		content = content.replaceAll("\\[thead\\]", getThead(fields));
		content = content.replaceAll("\\[tbody\\]", getTbody(fields));
		content = content.replaceAll("\\[table_name\\]", tableName);
		content = content.replaceAll("\\[bean_name\\]", beanName);
		content = content.replaceAll("\\[nick_name\\]", nickName);
		content = content.replaceAll("\\[head_name\\]", headName);
		return content;
	}

	public <E> String generateEdit(Class<E> target) throws Exception {
		Field[] fields = target.getDeclaredFields();
		File file = new File(this.getClass().getResource("/template/edit.template").getFile());
		String content = FileUtil.read(file);
		content = content.replaceAll("\\[tscript\\]", getTscript(fields));
		content = content.replaceAll("\\[tedit\\]", getTedit(fields));
		content = content.replaceAll("\\[table_name\\]", tableName);
		return content;
	}

	public <E> String generateSave(Class<E> target) throws Exception {
		File file = new File(this.getClass().getResource("/template/save.template").getFile());
		String content = FileUtil.read(file);
		content = content.replaceAll("\\[table_name\\]", tableName);
		content = content.replaceAll("\\[nick_name\\]", nickName);
		return content;
	}
	
	public <E> String generateInfo(Class<E> target) throws Exception {
		File file = new File(this.getClass().getResource("/template/info.template").getFile());
		String content = FileUtil.read(file);
		content = content.replaceAll("\\[table_name\\]", tableName);
		content = content.replaceAll("\\[nick_name\\]", nickName);
		return content;
	}

	public <E> String generateShow(Class<E> target) throws Exception {
		Field[] fields = target.getDeclaredFields();
		File file = new File(this.getClass().getResource("/template/show.template").getFile());
		String content = FileUtil.read(file);
		content = content.replaceAll("\\[tshow\\]", getTshow(fields));
		return content;
	}

	public <E> String generateAction(Class<E> target) throws Exception {
		Field[] fields = target.getDeclaredFields();
		File file = new File(this.getClass().getResource("/template/action.template").getFile());
		String content = FileUtil.read(file);
		content = content.replaceAll("\\[tfield\\]", getField(fields));
		content = content.replaceAll("\\[tgetset\\]", getGetset(fields));
		content = content.replaceAll("\\[tsave\\]", getSave(tableName, fields));
		content = content.replaceAll("\\[tlist\\]", getList(tableName, fields));
		content = content.replaceAll("\\[export_head\\]", getHeadExport(fields));
		content = content.replaceAll("\\[export_body\\]", getBodyExport(tableName, fields));
		content = content.replaceAll("\\[table_name\\]", tableName);
		content = content.replaceAll("\\[bean_name\\]", beanName);
		content = content.replaceAll("\\[nick_name\\]", nickName);
		content = content.replaceAll("\\[head_name\\]", headName);
		return content;
	}

	public String getCondition(Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();
		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String name = field.getName();
			String type = field.getGenericType().getTypeName();
			if (type.equals("java.lang.String")) {
				if (field.getAnnotation(FieldMeta.class).length() == 1) {
					sb.append("<s:select id=\"" + name + "\" name=\"" + name + "\" list=\"#{'1':'有效','0':'无效'}\" headerKey=\"\" headerValue=\""+field.getAnnotation(FieldMeta.class).name()+"\"/>");
					sb.append(System.lineSeparator());
				} else {
					sb.append("<s:textfield  id=\"" + name + "\" name=\"" + name + "\" placeholder=\""+field.getAnnotation(FieldMeta.class).name()+"\"></s:textfield>");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.lang.Long")) {
				if (field.getAnnotation(FieldMeta.class).range()) {
					sb.append("<s:textfield id=\"" + name + "Top\" name=\"" + name + "Top\" placeholder=\""+field.getAnnotation(FieldMeta.class).name()+"起\"></s:textfield> - <s:textfield name=\"" + name + "End\" id=\"" + name + "End\" placeholder=\""+field.getAnnotation(FieldMeta.class).name()+"止\"></s:textfield>");
					sb.append(System.lineSeparator());
				} else {
					sb.append("<s:textfield id=\"" + name + "\" name=\"" + name + "\" placeholder=\""+field.getAnnotation(FieldMeta.class).name()+"\"></s:textfield>");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.lang.Double")) {
				if (field.getAnnotation(FieldMeta.class).range()) {
					sb.append("<s:textfield id=\"" + name + "Top\" name=\"" + name + "Top\" placeholder=\""+field.getAnnotation(FieldMeta.class).name()+"起\"></s:textfield> - <s:textfield name=\"" + name + "End\" id=\"" + name + "End\" placeholder=\""+field.getAnnotation(FieldMeta.class).name()+"止\"></s:textfield>");
					sb.append(System.lineSeparator());
				} else {
					sb.append("<s:textfield id=\"" + name + "\" name=\"" + name + "\" placeholder=\""+field.getAnnotation(FieldMeta.class).name()+"\"></s:textfield>");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.util.Date")) {
				sb.append(System.lineSeparator());
				if (field.getAnnotation(FieldMeta.class).range()) {
					sb.append("<s:textfield  id=\"" + name + "Top\"  name=\"" + name + "Top\" placeholder=\""+field.getAnnotation(FieldMeta.class).name()+"起\"  onclick=\"WdatePicker();\"  cssStyle=\"width:100px;\" ><s:param name=\"value\"><s:date name=\"" + name
							+ "Top\" format=\"yyyy-MM-dd\"/></s:param></s:textfield>");
					sb.append(System.lineSeparator());
					sb.append(" - ");
					sb.append(System.lineSeparator());
					sb.append("<s:textfield  id=\"" + name + "End\"  name=\"" + name + "End\" placeholder=\""+field.getAnnotation(FieldMeta.class).name()+"止\"  onclick=\"WdatePicker();\"  cssStyle=\"width:100px;\" ><s:param name=\"value\"><s:date name=\"" + name
							+ "End\" format=\"yyyy-MM-dd\"/></s:param></s:textfield>");
					sb.append(System.lineSeparator());
				} else {
					sb.append("<s:textfield  id=\"" + name + "\"  name=\"" + name + "\" placeholder=\""+field.getAnnotation(FieldMeta.class).name()+"\" onclick=\"WdatePicker();\"  cssStyle=\"width:100px;\" ><s:param name=\"value\"><s:date name=\"" + name
							+ "\" format=\"yyyy-MM-dd\"/></s:param></s:textfield>");
					sb.append(System.lineSeparator());
				}
			}
		}
		return sb.toString();
	}

	public String getSort(Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();
		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String name = field.getName();
			sb.append("<input type=\"button\" class=\"submit\" value=\"" + field.getAnnotation(FieldMeta.class).name() + "\" onclick=\"toSort('sortField','" + name + "')\"/>");
			sb.append(System.lineSeparator());
		}
		return sb.toString();
	}

	public String getThead(Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("<th data-options=\"field:'ck'\" checkbox=\"true\"></th>");
		sb.append(System.lineSeparator());
		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String name = field.getName();
			sb.append("<th data-options=\"field:'" + name + "', width:'10%',align:'left' , halign: 'left'\">" + field.getAnnotation(FieldMeta.class).name() + "</th>");
			sb.append(System.lineSeparator());
		}
		sb.append("<th data-options=\"field:'op', width:'100',align:'left' , halign: 'left'\">操作</th>");
		sb.append(System.lineSeparator());
		return sb.toString();
	}

	public String getTbody(Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();
		sb.append("<td><s:property value=\"#show." + nickName + "No\"/></td>");
		sb.append(System.lineSeparator());

		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String name = field.getName();
			String type = field.getGenericType().getTypeName();
			if (type.equals("java.lang.String")) {
				if (field.getAnnotation(FieldMeta.class).length() == 1) {
					sb.append("<td><s:if test=\"#show." + name + "==0\"><img src=\"<%=request.getContextPath() %>/image/cancel.png\"  /></s:if><s:else><img src=\"<%=request.getContextPath() %>/image/accept.png\"  /></s:else></td>");
					sb.append(System.lineSeparator());
				} else {
					sb.append("<td><s:property value=\"#show." + name + "\"/></td>");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.lang.Long")) {
				sb.append("<td><s:text name=\"global.format.count\"><s:param value=\"#show." + name + "\"/></s:text></td>");
				sb.append(System.lineSeparator());
			}
			if (type.equals("java.lang.Double")) {
				sb.append("<td><s:text name=\"global.format.number\"><s:param value=\"#show." + name + "\"/></s:text></td>");
				sb.append(System.lineSeparator());
			}
			if (type.equals("java.util.Date")) {
				sb.append("<td><s:text name=\"global.format.dateminute\"><s:param value=\"#show." + name + "\"/></s:text>");
				sb.append(System.lineSeparator());
			}
		}
		sb.append("<td>");
		sb.append(System.lineSeparator());
		sb.append("<input type=\"button\" value=\"查看\" style=\"width: 50px;\" class=\"submit\" onclick=\"location.href='<%=request.getContextPath()%>/" + tableName + "_show.html?" + nickName + "No=<s:property value=\"#show." + nickName
				+ "No\"/>'\"/>");
		sb.append(System.lineSeparator());
		sb.append("<input type=\"button\" value=\"修改\" style=\"width: 50px;\"class=\"submit\" onclick=\"location.href='<%=request.getContextPath()%>/" + tableName + "_edit.html?" + nickName + "No=<s:property value=\"#show." + nickName
				+ "No\"/>'\"/>");
		sb.append(System.lineSeparator());
		sb.append("<input type=\"button\" value=\"删除\" style=\"width: 50px;\"class=\"submit\" onclick=\"location.href='<%=request.getContextPath()%>/" + tableName + "_move.html?" + nickName + "No=<s:property value=\"#show." + nickName
				+ "No\"/>'\"/>");
		sb.append(System.lineSeparator());
		sb.append("</td>");
		sb.append(System.lineSeparator());
		return sb.toString();
	}

	public String getTscript(Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();

		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String name = field.getName();
			String type = field.getGenericType().getTypeName();
			if (type.equals("java.lang.String")) {
				if (field.getAnnotation(FieldMeta.class).require()) {
					sb.append(name + " : {	required : true	},");
					sb.append(System.lineSeparator());
				} else {
					sb.append(name + " : {	required : false	},");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.lang.Long")) {
				if (field.getAnnotation(FieldMeta.class).require()) {
					sb.append(name + " : {	required : true,number : true},");
					sb.append(System.lineSeparator());
				} else {
					sb.append(name + " : {	required : false	,number : true},");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.lang.Double")) {
				if (field.getAnnotation(FieldMeta.class).require()) {
					sb.append(name + " : {	required : true,number : true},");
					sb.append(System.lineSeparator());
				} else {
					sb.append(name + " : {	required : false	,number : true},");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.util.Date")) {
				if (field.getAnnotation(FieldMeta.class).require()) {
					sb.append(name + " : {	required : true	},");
					sb.append(System.lineSeparator());
				} else {
					sb.append(name + " : {	required : false	},");
					sb.append(System.lineSeparator());
				}
			}
		}
		sb.append(System.lineSeparator());
		String s = sb.toString();
		s = s.substring(0, s.lastIndexOf(","));
		return s;
	}

	public String getTedit(Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();

		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String name = field.getName();
			String type = field.getGenericType().getTypeName();
			if (type.equals("java.lang.String")) {
				if (field.getAnnotation(FieldMeta.class).length() == 1) {
					sb.append("<tr><td height=\"30\" align=\"right\">" + field.getAnnotation(FieldMeta.class).name() + " : </td><td><s:radio id=\"" + name + "\" name=\"" + name + "\" list=\"#{'0':'无效','1':'有效'}\"  value=\"" + tableName
							+ "Bean." + name + "\"></s:radio></td><td>" + field.getAnnotation(FieldMeta.class).description() + "</td></tr>");
					sb.append(System.lineSeparator());
				} else {
					sb.append("<tr><td height=\"30\" align=\"right\">" + field.getAnnotation(FieldMeta.class).name() + " : </td><td><input type=\"text\" placeholder=\"" + field.getAnnotation(FieldMeta.class).name() + "\" name=\"" + name
							+ "\" id=\"" + name + "\" value=\"<s:property value=\"" + tableName + "Bean." + name + "\"/>\"/></td><td>" + field.getAnnotation(FieldMeta.class).description() + "</td></tr>");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.lang.Long")) {
				sb.append("<tr><td height=\"30\" align=\"right\">" + field.getAnnotation(FieldMeta.class).name() + " : </td>");
				sb.append("<td><input type=\"text\" placeholder=\"" + field.getAnnotation(FieldMeta.class).name() + "\" name=\"" + name + "\" id=\"" + name + "\" value=\"<s:if test=\""+tableName+"Bean."+name+"!=null\"><s:text name=\"global.format.count\"><s:param value=\"" + tableName
						+ "Bean." + name + "\"/></s:text></s:if>\" onkeyup=\"value=value.replace(/[^\\\\d\\\\.]/g,'')\"/></td>");
				sb.append("<td>" + field.getAnnotation(FieldMeta.class).description() + "</td></tr>");
				sb.append(System.lineSeparator());
			}
			if (type.equals("java.lang.Double")) {
				sb.append("<tr><td height=\"30\" align=\"right\">" + field.getAnnotation(FieldMeta.class).name() + " : </td>");
				sb.append("<td><input type=\"text\" placeholder=\"" + field.getAnnotation(FieldMeta.class).name() + "\" name=\"" + name + "\" id=\"" + name + "\" value=\"<s:if test=\""+tableName+"Bean."+name+"!=null\"><s:text name=\"global.format.number\"><s:param value=\"" + tableName
						+ "Bean." + name + "\"/></s:text></s:if>\" onkeyup=\"value=value.replace(/[^\\\\d\\\\.]/g,'')\" /></td>");
				sb.append("<td>" + field.getAnnotation(FieldMeta.class).description() + "</td></tr>");
				sb.append(System.lineSeparator());
			}
			if (type.equals("java.util.Date")) {
				sb.append("<tr><td height=\"30\" align=\"right\">" + field.getAnnotation(FieldMeta.class).name() + " : </td>");
				sb.append("<td><input type=\"text\" placeholder=\"" + field.getAnnotation(FieldMeta.class).name() + "\" name=\"" + name + "\" id=\"" + name + "\" value=\"<s:if test=\""+tableName+"Bean."+name+"!=null\"><s:text name=\"global.format.date\"><s:param value=\"" + tableName
						+ "Bean." + name + "\"/></s:text></s:if>\" onclick=\"WdatePicker();\" /></td>");
				sb.append("<td>" + field.getAnnotation(FieldMeta.class).description() + "</td></tr>");
				sb.append(System.lineSeparator());
			}
		}
		sb.append(System.lineSeparator());
		return sb.toString();
	}

	public String getField(Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();
		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String name = field.getName();
			String type = field.getGenericType().getTypeName();
			if (type.equals("java.lang.String")) {
				sb.append("private String " + name + ";");
				sb.append(System.lineSeparator());
				if (field.getAnnotation(FieldMeta.class).range()) {
					sb.append("private String " + name + "s;");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.lang.Long")) {
				sb.append("private Long " + name + ";");
				sb.append(System.lineSeparator());
				if (field.getAnnotation(FieldMeta.class).range()) {
					sb.append("private Long " + name + "Top;");
					sb.append(System.lineSeparator());
					sb.append("private Long " + name + "End;");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.lang.Double")) {
				sb.append("private Double " + name + ";");
				sb.append(System.lineSeparator());
				if (field.getAnnotation(FieldMeta.class).range()) {
					sb.append("private Double " + name + "Top;");
					sb.append(System.lineSeparator());
					sb.append("private Double " + name + "End;");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.util.Date")) {
				sb.append("private Date " + name + ";");
				sb.append(System.lineSeparator());
				if (field.getAnnotation(FieldMeta.class).range()) {
					sb.append("private Date " + name + "Top;");
					sb.append(System.lineSeparator());
					sb.append("private Date " + name + "End;");
					sb.append(System.lineSeparator());
				}
			}
		}
		return sb.toString();
	}

	public String getGetset(Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();
		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String name = field.getName();
			String type = field.getGenericType().getTypeName();
			if (type.equals("java.lang.String")) {
				sb.append("public String get" + StringUtil.upperHead(name) + "() {	return " + name + ";}");
				sb.append(System.lineSeparator());
				sb.append("public void set" + StringUtil.upperHead(name) + "(String " + name + ") {this." + name + " = " + name + ";	}");
				sb.append(System.lineSeparator());
				if (field.getAnnotation(FieldMeta.class).range()) {
					sb.append("public String get" + StringUtil.upperHead(name) + "s() {	return " + name + "s;}");
					sb.append(System.lineSeparator());
					sb.append("public void set" + StringUtil.upperHead(name) + "s(String " + name + "s) {this." + name + "s = " + name + "s;	}");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.lang.Long")) {
				sb.append("public Long get" + StringUtil.upperHead(name) + "() {	return " + name + ";}");
				sb.append(System.lineSeparator());
				sb.append("public void set" + StringUtil.upperHead(name) + "(Long " + name + ") {this." + name + " = " + name + ";	}");
				sb.append(System.lineSeparator());
				if (field.getAnnotation(FieldMeta.class).range()) {
					sb.append("public Long get" + StringUtil.upperHead(name) + "Top() {	return " + name + "Top;}");
					sb.append(System.lineSeparator());
					sb.append("public void set" + StringUtil.upperHead(name) + "Top(Long " + name + "Top) {this." + name + "Top = " + name + "Top;	}");
					sb.append(System.lineSeparator());
					sb.append("public Long get" + StringUtil.upperHead(name) + "End() {	return " + name + "End;}");
					sb.append(System.lineSeparator());
					sb.append("public void set" + StringUtil.upperHead(name) + "End(Long " + name + "End) {this." + name + "End = " + name + "End;	}");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.lang.Double")) {
				sb.append("public Double get" + StringUtil.upperHead(name) + "() {	return " + name + ";}");
				sb.append(System.lineSeparator());
				sb.append("public void set" + StringUtil.upperHead(name) + "(Double " + name + ") {this." + name + " = " + name + ";	}");
				sb.append(System.lineSeparator());
				if (field.getAnnotation(FieldMeta.class).range()) {
					sb.append("public Double get" + StringUtil.upperHead(name) + "Top() {	return " + name + "Top;}");
					sb.append(System.lineSeparator());
					sb.append("public void set" + StringUtil.upperHead(name) + "Top(Double " + name + "Top) {this." + name + "Top = " + name + "Top;	}");
					sb.append(System.lineSeparator());
					sb.append("public Double get" + StringUtil.upperHead(name) + "End() {	return " + name + "End;}");
					sb.append(System.lineSeparator());
					sb.append("public void set" + StringUtil.upperHead(name) + "End(Double " + name + "End) {this." + name + "End = " + name + "End;	}");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.util.Date")) {
				sb.append("public Date get" + StringUtil.upperHead(name) + "() {	return " + name + ";}");
				sb.append(System.lineSeparator());
				sb.append("public void set" + StringUtil.upperHead(name) + "(Date " + name + ") {this." + name + " = " + name + ";	}");
				sb.append(System.lineSeparator());
				if (field.getAnnotation(FieldMeta.class).range()) {
					sb.append("public Date get" + StringUtil.upperHead(name) + "Top() {	return " + name + "Top;}");
					sb.append(System.lineSeparator());
					sb.append("public void set" + StringUtil.upperHead(name) + "Top(Date " + name + "Top) {this." + name + "Top = " + name + "Top;	}");
					sb.append(System.lineSeparator());
					sb.append("public Date get" + StringUtil.upperHead(name) + "End() {	return " + name + "End;}");
					sb.append(System.lineSeparator());
					sb.append("public void set" + StringUtil.upperHead(name) + "End(Date " + name + "End) {this." + name + "End = " + name + "End;	}");
					sb.append(System.lineSeparator());
				}
			}
		}
		return sb.toString();
	}

	public String getSave(String tableName, Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();
		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String name = field.getName();
			sb.append(tableName + "Bean.set" + StringUtil.upperHead(name) + "(" + name + ");");
			sb.append(System.lineSeparator());

		}
		return sb.toString();
	}
	
	public String getList(String tableName, Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();
		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String name = field.getName();
			String type = field.getGenericType().getTypeName();
			if (type.equals("java.lang.String")) {
				if(field.getAnnotation(FieldMeta.class).primary()){
					sb.append("paramMap.put(\""+name+"\", StringUtil.split("+name+"));");
				}else{
					sb.append("paramMap.put(\""+name+"\", "+name+");");
				}
			}
			if (type.equals("java.lang.Long")) {
					sb.append("paramMap.put(\""+name+"\", "+name+");");
			}
			if (type.equals("java.lang.Double")) {
					sb.append("paramMap.put(\""+name+"\", "+name+");");
			}
			if (type.equals("java.util.Date")) {
				sb.append("paramMap.put(\""+name+"\", ParameterUtil.dateCombin("+name+"Top, "+name+"End));");
			}
			sb.append(System.lineSeparator());

		}
		return sb.toString();
	}

	public String getHeadExport(Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();
		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String type = field.getGenericType().getTypeName();
			if (type.equals("java.util.Date")) {
				sb.append("fieldHead.add(\"" + field.getAnnotation(FieldMeta.class).name() + "日期\");");
				sb.append(System.lineSeparator());
				sb.append("fieldHead.add(\"" + field.getAnnotation(FieldMeta.class).name() + "时间\");");
				sb.append(System.lineSeparator());
			} else {
				sb.append("fieldHead.add(\"" + field.getAnnotation(FieldMeta.class).name() + "\");");
				sb.append(System.lineSeparator());
			}
		}
		return sb.toString();
	}

	public String getBodyExport(String tableName, Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();
		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String name = field.getName();
			String type = field.getGenericType().getTypeName();
			if (type.equals("java.lang.String")) {
				if (field.getAnnotation(FieldMeta.class).length() == 1) {
					sb.append("fieldItem.add((" + tableName + "Bean.get" + StringUtil.upperHead(name) + "().equals(\"0\"))?\"无效\":\"有效\");");
					sb.append(System.lineSeparator());
				} else {
					sb.append("fieldItem.add(" + tableName + "Bean.get" + StringUtil.upperHead(name) + "());");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.lang.Long")) {
				sb.append("fieldItem.add(" + tableName + "Bean.get" + StringUtil.upperHead(name) + "().toString());");
				sb.append(System.lineSeparator());
			}
			if (type.equals("java.lang.Double")) {
				sb.append("fieldItem.add(" + tableName + "Bean.get" + StringUtil.upperHead(name) + "().toString());");
				sb.append(System.lineSeparator());
			}
			if (type.equals("java.util.Date")) {
				sb.append("fieldItem.add(DateUtil.format(" + tableName + "Bean.get" + StringUtil.upperHead(name) + "(),DateUtil.DATE));");
				sb.append(System.lineSeparator());
				sb.append("fieldItem.add(DateUtil.format(" + tableName + "Bean.get" + StringUtil.upperHead(name) + "(),DateUtil.TIME));");
				sb.append(System.lineSeparator());
			}
		}
		return sb.toString();
	}

	public String getTshow(Field[] fields) throws Exception {
		StringBuffer sb = new StringBuffer();

		for (Field field : fields) {
			if (field.getName().equals("serialVersionUID")) {
				continue;
			}
			String name = field.getName();
			String type = field.getGenericType().getTypeName();
			if (type.equals("java.lang.String")) {
				if (field.getAnnotation(FieldMeta.class).length() == 1) {
					sb.append("<tr><td height=\"30\" align=\"right\">" + field.getAnnotation(FieldMeta.class).name() + " : </td><td><s:if test=\"" + tableName + "Bean." + name
							+ "==0\"><font color=\"red\">无效</font></s:if><s:else><font color=\"green\">有效</font></s:else></td><td>" + field.getAnnotation(FieldMeta.class).description() + "</td></tr>");
					sb.append(System.lineSeparator());
				} else {
					sb.append("<tr><td height=\"30\" align=\"right\">" + field.getAnnotation(FieldMeta.class).name() + " : </td><td><s:property value=\"@utils.StringUtil@limit(" + tableName + "Bean." + name + ",20)\"/></td><td>"
							+ field.getAnnotation(FieldMeta.class).description() + "</td></tr>");
					sb.append(System.lineSeparator());
				}
			}
			if (type.equals("java.lang.Long")) {
				sb.append("<tr><td height=\"30\" align=\"right\">" + field.getAnnotation(FieldMeta.class).name() + " : </td><td><s:text name=\"global.format.count\"><s:param value=\"" + tableName + "Bean." + name
						+ "\" /></s:text></td><td>" + field.getAnnotation(FieldMeta.class).description() + "</td></tr>");
				sb.append(System.lineSeparator());
			}
			if (type.equals("java.lang.Double")) {
				sb.append("<tr><td height=\"30\" align=\"right\">" + field.getAnnotation(FieldMeta.class).name() + " : </td><td><s:text name=\"global.format.number\"><s:param value=\"" + tableName + "Bean." + name
						+ "\" /></s:text></td><td>" + field.getAnnotation(FieldMeta.class).description() + "</td></tr>");
				sb.append(System.lineSeparator());
			}
			if (type.equals("java.util.Date")) {
				sb.append("<tr><td height=\"30\" align=\"right\">" + field.getAnnotation(FieldMeta.class).name() + " : </td><td><s:text name=\"global.format.datetime\"><s:param value=\"" + tableName + "Bean." + name
						+ "\" /></s:text></td><td>" + field.getAnnotation(FieldMeta.class).description() + "</td></tr>");
				sb.append(System.lineSeparator());
			}
		}
		sb.append(System.lineSeparator());
		return sb.toString();
	}

	public static void main(String[] args) throws Exception {
		//new TemplateUtil("merchant", "Merchant", "mer", "Mer", "d://").generate(TMerchant.class);
//		 new TemplateUtil("transaction", "Transaction", "txn", "Txn", "d://").generate(TTransaction.class);
//		new TemplateUtil("bank", "Bank", "bnk", "Bnk","d://").generate(TBank.class);
//		 new TemplateUtil("card", "Card", "crd", "Crd", "d://").generate(TCard.class);
//		 new TemplateUtil("subject", "Subject", "sub", "Sub", "d://").generate(TSubject.class);
//		 new TemplateUtil("website", "Website", "web", "Web", "d://").generate(TWebsite.class);
		//new TemplateUtil("currency", "Currency", "cur", "Cur", "d://").generate(TCurrency.class);
//		 new TemplateUtil("bin", "Bin", "bin", "Bin", "d://").generate(TBin.class);
//		new TemplateUtil("bc", "Bc", "bc", "Bc", "d://").generate(TBc.class);
//		new TemplateUtil("cb", "Cb", "cb", "Cb", "d://").generate(TCb.class);
//		new TemplateUtil("cs", "Cs", "cs", "Cs", "d://").generate(TCs.class);
//		new TemplateUtil("sc", "Sc", "sc", "Sc", "d://").generate(TSc.class);
//		new TemplateUtil("sm", "Sm", "sm", "Sm", "d://").generate(TSm.class);
//		new TemplateUtil("risk", "Risk", "rsk", "Rsk", "d://").generate(TRisk.class);
//		new TemplateUtil("csm", "Csm", "csm", "Csm", "d://").generate(TCsm.class);
//		new TemplateUtil("blackip", "Blackip", "bck", "Bck", "d://").generate(TBlackip.class);
//		new TemplateUtil("mail", "Mail", "mal", "Mal", "d://").generate(TMail.class);
//		new TemplateUtil("intercept", "Intercept", "inc", "Inc", "d://").generate(TIntercept.class);
//		new TemplateUtil("logicTransaction", "Logic", "log", "Log", "d://").generate(VLogicTransaction.class);
//		new TemplateUtil("msm", "Msm", "msm", "Msm", "d://").generate(TMsm.class);
//		new TemplateUtil("mb", "Mb", "mb", "Mb", "d://").generate(TMb.class);
//		new TemplateUtil("bm", "Bm", "bm", "Bm", "d://").generate(TBm.class);
//		new TemplateUtil("balance", "Balance", "bal", "Bal", "d://").generate(TBalance.class);
//		new TemplateUtil("settle", "Settle", "ste", "Ste", "d://").generate(TSettle.class);
//		new TemplateUtil("link", "Link", "lnk", "Lnk", "d://").generate(TLink.class);
//		
//		new TemplateUtil("product", "Product", "pct", "Pct", "d://").generate(TProduct.class);
//		new TemplateUtil("replace", "Replace", "rep", "Rep", "d://").generate(TReplace.class);
//		new TemplateUtil("ips", "Ips", "ips", "Ips", "d://").generate(TIps.class);
//		new TemplateUtil("mcc", "Mcc", "mcc", "Mcc", "d://").generate(TMcc.class);
		new TemplateUtil("brand", "Brand", "bnd", "Bnd", "d://").generate(TBrand.class);
	}

}
